package c.w.base.proxy;
/**************打车用户基本数据操作（增、改、查）*****************/
import java.util.ArrayList;

import c.w.base.dto.Person;
import c.w.base.dto.PersonProductNumber;
import c.w.db.ConnDB;


public class PersonBaseOperation {
	/**
	 * 公共变量
	 */
	ConnDB db = new ConnDB();
	
	/**
	 * 添加一个打车用户
	 * @author PHY
	 * @param p_city  打车用户所在城市
	 * @param p_id  打车用户id
	 * @param cellphone_number  手机号
	 * @param consumption_score  消费积分
	 * @param integrity_score  诚信积分
	 * @param integrity_id  外键——与诚信表关联，是诚信表id：主键
	 * @return true:添加成功		false:添加失败
	 */
	public boolean addPerson(String p_city, int p_id, String p_password, String cellphone_number, long consumption_score, long integrity_score, int integrity_id){
		boolean isAddPerson = db.addPerson(p_city, p_id, p_password, cellphone_number, consumption_score, integrity_score, integrity_id);
		boolean isAddIntegrity = db.addPersonIntegrity(p_city, integrity_id);
		if (isAddPerson && isAddIntegrity){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 添加打车用户的数据统计
	 * @author PHY
	 * @param p_city 打车用户所在城市
	 * @param p_id	打车用户id
	 * @param active_time  激活时间
	 * @param login_time  登录时间
	 * @param login_city  登录城市
	 * @param login_IP  登录IP
	 * @param share_car_times  拼车次数
	 * @return
	 */
	public boolean addPersonDatacount(String p_city, int p_id, String active_time, String login_time, String login_city, String login_IP,long share_car_times){
		boolean isAddPersonDatacount = db.addPersonDataCount(p_city, p_id, active_time, login_time, login_city, login_IP, share_car_times);
		if (isAddPersonDatacount){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新打车用户数据统计
	 * @param p_city 打车用户所在城市
	 * @param p_id 打车用户id
	 * @param login_time 登录时间
	 * @param login_city 登录城市
	 * @param login_IP 登录IP
	 * @param share_car_times 拼车次数
	 * @return
	 */
	public boolean updatePersonDatacount(String p_city, int p_id, String login_time, String login_city, String login_IP, long share_car_times){
		boolean isUpdateDatacount = db.updatePersonDatacount(p_city, p_id, login_time, login_city, login_IP, share_car_times);
		if (isUpdateDatacount){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新一个打车用户消费积分和诚信积分
	 * @author PHY
	 * @param p_city  打车用户所在城市
	 * @param p_id  打车用户id
	 * @param consumption_score  消费积分
	 * @param integrity_score  诚信积分
	 * @return true:更新成功		false:更新失败
	 */
	public boolean updatePersonConsumptionAndIntegrityScore(String p_city, int p_id, long consumption_score, long integrity_score){
		boolean isUpdate = db.updatePersonConsumption_scoreAndIntegrityScore(p_city, p_id, consumption_score, integrity_score);
		if (isUpdate){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新一个打车用户诚信度
	 * @author PHY
	 * @param p_city  打车用户所在城市
	 * @param integrity_id  诚信度id
	 * @param integrity  诚信度
	 * @param exposed_times  被举报次数
	 * @return true:更新成功		false:更新失败
	 */
	public boolean updatePersonIntegrity(String p_city, int integrity_id, long integrity, int exposed_times){
		boolean isUpdate = db.updatePersonIntegrity(p_city, integrity_id, integrity, exposed_times);
		if (isUpdate){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 得到一个打车用户的所有信息
	 * @author PHY
	 * @param p_city  打车用户所在城市
	 * @param p_id	打车用户id,主键
	 *  @param p_password 密码
	 * @return result：打车用户信息结果集		null：空结果集
	 */
	public ArrayList<Person> getPersonAllInformation(String p_city, int p_id, String p_password){
		ArrayList<Person> result = db.getPersonAllInformation(p_city, p_id, p_password);
		if (result != null){
			return result;
		}else{
			return null;
		}
	}
	
	/**
	 * 得到打车用户的产品编号及版本
	 * @author PHY
	 * @param id  打车用户产品编号表id,主键
	 * @return result：打车用户产品编号信息结果集		null：空结果集
	 */
	public ArrayList<PersonProductNumber> getPersonProductNumber(int id){
		ArrayList<PersonProductNumber> result = db.getPersonProductNumber(id);
		if (result != null){
			return result;
		}else{
			return null;
		}
	}
	
	/**
	 * 得到打车用户等级
	 * @author PHY
	 * @param p_city 打车用户所在城市
	 * @param p_id 打车用户id
	 * @param p_password 密码
	 * @return grade: 等级
	 */
	public long getPersonGrade(String p_city, int p_id, String p_password){
		ArrayList<Person>  arrayListPerson = getPersonAllInformation(p_city, p_id, p_password);
		long consumption_score = 0;
		long integrity_score = 0;
		for (int i=0;i<arrayListPerson.size();i++){
			Person p = arrayListPerson.get(i);
			consumption_score = p.getConsumption_score();
			integrity_score = p.getIntegrity_score();
		}
		long grade = (long)(consumption_score*0.4 + integrity_score*0.6);
		
		return grade;
	}
}
